//
//  JFImageHelper.m
//  JFBase
//
//  Created by Alan Wang on 2018/11/29.
//  Copyright © 2018 Alan Wang. All rights reserved.
//

#import "JFImageHelper.h"
#import "JFMacro.h"


@implementation JFImageHelper


+(UIImage *) imageByCurrentScreen
{
     @autoreleasepool {
     UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, NO, 0);

    CGContextRef context = UIGraphicsGetCurrentContext();
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            CGContextConcatCTM(context, [window transform]);
            if (@available(iOS 7.0, *))
            {
                CGContextTranslateCTM(context,
                                      -[window bounds].size.width * [[window layer] anchorPoint].x,
                                      -[window bounds].size.height * [[window layer] anchorPoint].y);
            }
            else
            {
                CGContextTranslateCTM(context,
                                      -[window bounds].size.width * [[window layer] anchorPoint].x,
                                      -[window bounds].size.height * [[window layer] anchorPoint].y - 20);
            }
            [[window layer] renderInContext:context];
            window.layer.contents = nil;
            CGContextRestoreGState(context);
        }
    }
   
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    }
}

+ (UIImage *)imageByColor:(UIColor *)color size:(CGSize)size
{
    CGRect rect = CGRectMake(0, 0, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //填充颜色
    CGContextSetFillColorWithColor(context, [color CGColor]);
    //画图
    CGContextFillRect(context, rect);
    //获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
+(nonnull UIImage*)modifyImageSize:(CGSize)size OfImage:(nonnull UIImage*)image
{
    if(CGSizeEqualToSize(image.size, size))
    {
        return image;
    }
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

+(UIImage*)qrCodeWithString:(NSString*)str size:(CGSize)size logo:(UIImage*)logo logoSize:(CGSize)logosize{
    //创建过滤器
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSString *string = str;
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    [filter setValue:data forKeyPath:@"inputMessage"];
    CIImage *image = [filter outputImage];
    
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size.width/CGRectGetWidth(extent), size.height / CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    UIImage * qrimage = [UIImage imageWithCGImage:scaledImage];
    
    
    
    UIGraphicsBeginImageContext(qrimage.size);
    [qrimage drawInRect:CGRectMake(0, 0, qrimage.size.width, qrimage.size.height)];
    
    if (logo) {
        UIImage *smallImage = logo;
        CGSize tempSize = logosize;
        if (logosize.width == 0 || logosize.height == 0) {
            tempSize = smallImage.size;
        }
        [smallImage drawInRect:CGRectMake((qrimage.size.width - tempSize.width) / 2, (qrimage.size.width - tempSize.height) / 2, tempSize.width, tempSize.height)];
    }
 
    
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return finalImage;
    
}
@end
